<?php
/**
 * @file
 * JavaScript for header module.
 *
 * Usage:
 *  - A page that wants to use this JavaScript file should link to this file
 *    using the '<script></script>' tags once and only once.
 *
 * Parameter(s):
 *  - None.
 *
 * Global variable(s) set:
 *  - None.
 *
 * Global variable(s) unset:
 *  - None.
 *
 * Exception(s):
 *  - None.
 *
 * This file includes/requires:
 *  - h_greenindeed.php
 *  - h_header.php
 */

// header file(s) ==============================================================
require_once "h_greenindeed.php";
require_once "h_header.php";


// implementation ==============================================================
?>
// <script> // to induce syntax highlighting on netbeans

// implementation --------------------------------------------------------------
/**
 * Initializes the header module.
 */
function header_init() {
  // onclick event binding for header links (jquery)
  $(".<?php echo CSS_CLASS_HEADER_LINK ?>").click(function () {
    this.href += "?" + <?php echo JS_VAR_PARAMS_GET ?>;
  });
  $(".<?php echo CSS_CLASS_HEADER_LOCALE ?>").change(function () {
    // get start index of locale parameter in GET request
    var pos_start
      = <?php echo JS_VAR_PARAMS_GET ?>.indexOf("<?php echo PARAM_LOCALE ?>=");

    // update locale value
    if (-1 == pos_start) {
      // locale parameter not in GET request => prepend to GET request
      <?php echo JS_VAR_PARAMS_GET ?>
        = "<?php echo PARAM_LOCALE ?>=" + this.value + "&"
        + <?php echo JS_VAR_PARAMS_GET ?>;
    } else {
      // update locale value

      // get start index of locale value
      pos_start += "<?php echo PARAM_LOCALE ?>".length + 1; // +1 for '=' sign
      // get end index of locale value
      var pos_end = <?php echo JS_VAR_PARAMS_GET ?>.indexOf("&", pos_start);
      if (-1 == pos_end) {
        // end index of locale value not found => value until end of string
        pos_end = <?php echo JS_VAR_PARAMS_GET ?>.length;
      }

      // replace locale value
      <?php echo JS_VAR_PARAMS_GET ?>
        = <?php echo JS_VAR_PARAMS_GET ?>.substr(0, pos_start)
        + this.value
        + <?php echo JS_VAR_PARAMS_GET ?>.substr(pos_end);
    }
    location.href = location.href.substr(0, location.href.indexOf("?"))
                  + "?" + <?php echo JS_VAR_PARAMS_GET ?>;
  });
}

// </script> // to induce syntax highlighting on netbeans
